package com.ruoyi.web.controller.gallery;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.supe.Substation;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.gallery.SubstationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.web.service.TokenService;
import java.util.List;

@RestController
@RequestMapping("/fenzhan/substation")
public class SubstationController extends BaseController {

    @Autowired
    private SubstationService substationService;
    @Autowired(required = false)
    private TokenService tokenService;

    @Autowired
    private SysUserMapper sysUserMapper;
    /**
     * 查询分站列表
     */
    @PreAuthorize("@ss.hasPermi('fenzhan:substation:list')")
    @GetMapping("/list")
    public TableDataInfo list(Substation substation)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        substation.setUserId(userId);
        List<Substation> list = substationService.selectSubstationList(substation);
        return getDataTable(list);
    }

    /**
     * 导出分站列表
     */
    @PreAuthorize("@ss.hasPermi('fenzhan:substation:export')")
    @Log(title = "分站", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Substation substation)
    {
        List<Substation> list = substationService.selectSubstationList(substation);
        ExcelUtil<Substation> util = new ExcelUtil<Substation>(Substation.class);
        return util.exportExcel(list, "substation");
    }

    /**
     * 获取分站详细信息
     */
    @PreAuthorize("@ss.hasPermi('fenzhan:substation:query')")
    @GetMapping(value = "/{substationId}")
    public AjaxResult getInfo(@PathVariable("substationId") Long substationId)
    {
        return AjaxResult.success(substationService.selectSubstationById(substationId));
    }

    /**
     * 新增分站
     */
    @PreAuthorize("@ss.hasPermi('fenzhan:substation:add')")
    @Log(title = "分站", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Substation substation)
    {

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        substation.setUserId(userId);
        SysUser aById = sysUserMapper.findAById(userId);
        Long deptId = aById.getDeptId();
        substation.setDeptId(deptId);
        if (UserConstants.NOT_UNIQUE.equals(substationService.checkUserIdUnique(substation.getUserId()))){
            return AjaxResult.error("已确认,是分站");
        }
        return toAjax(substationService.insertSubstation(substation));
    }

    /**
     * 修改分站
     */
    @PreAuthorize("@ss.hasPermi('fenzhan:substation:edit')")
    @Log(title = "分站", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Substation substation)
    {
        return toAjax(substationService.updateSubstation(substation));
    }

    /**
     * 删除分站
     */
    @PreAuthorize("@ss.hasPermi('fenzhan:substation:remove')")
    @Log(title = "分站", businessType = BusinessType.DELETE)
    @DeleteMapping("/{substationIds}")
    public AjaxResult remove(@PathVariable Long[] substationIds)
    {
        return toAjax(substationService.deleteSubstationByIds(substationIds));
    }

}
